package weaver.limitCurrent;

import java.io.File;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.log4j.Logger;
import weaver.general.GCONST;

public class LimitMap {
  private static final Logger LOGGER = Logger.getLogger(LimitMap.class);
  
  private static final Map<String, String> initMap = new ConcurrentHashMap<String, String>();
  
  public static Integer getInt(String key) {
    try {
      return Integer.valueOf(Integer.parseInt(get(key)));
    } catch (NumberFormatException e) {
      LOGGER.error("key:" + key + "+ get(key) + ");
      LOGGER.error(e.getClass().getName(), e);
      return null;
    } 
  }
  
  private static void init() {
    String PROP_ROOT = GCONST.getPropertyPath();
    String fileName = String.valueOf(PROP_ROOT) + "LimitCurrent.properties";
    File f = new File(fileName);
    Properties prop = PropUtil.getProperties(f);
    PropUtil.propTransIntoMap(initMap, prop);
  }
  
  public static String get(String key) {
    return get(key, "");
  }
  
  public static String get(String key, String defaultStr) {
    if (initMap.size() == 0)
      init(); 
    if (initMap.get(key) == null)
      return defaultStr; 
    return initMap.get(key);
  }
  
  public static Map<String, String> getAllMap() {
    if (initMap.size() == 0)
      init(); 
    return Collections.unmodifiableMap(initMap);
  }
  
  public static void reload() {
    try {
      initMap.clear();
      init();
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
}
